package com.vivebest.utils.app.controller;

import com.vivebest.utils.common.response.ResultBody;
import com.vivebest.utils.kd.service.K8sDeployService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * k8s部署
 * </p>
 *
 * @author zsk
 * @date 2020/8/13 下午4:54
 */
@RestController
@RequestMapping("/k8s")
public class K8sDeployController {

    @Autowired
    K8sDeployService k8sDeployService;

    @PostMapping("/uncompressTar")
    @ApiOperation("解压tar")
    public ResultBody uncompressTar(@ApiParam("工作目录") String workPath,@ApiParam("tar绝对路径") String file) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.uncompressTar(workPath, file));
    }

    @PostMapping("/createDockerImage")
    @ApiOperation("构建docker镜像")
    public ResultBody createDockerImage(String workPath, String tag) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.createDockerImage(workPath, tag));
    }

    @PostMapping("/pushDockerImage")
    @ApiOperation("推送docker镜像")
    public ResultBody pushDockerImage(String tag) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.pushDockerImage(tag));
    }

    @PostMapping("/createConfigMap")
    @ApiOperation("创建configmap")
    public ResultBody createConfigMap(String configFilePath, String configMapName, String namespace) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.createConfigMap(configFilePath, configMapName, namespace));
    }

    @PostMapping("/createK8sPod")
    @ApiOperation("创建pod")
    public ResultBody createK8sPod(String podFile) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.createK8sPod(podFile));
    }

    @PostMapping("/createK8sService")
    @ApiOperation("创建service")
    public ResultBody createK8sService(String serviceFile) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.createK8sService(serviceFile));
    }

    @DeleteMapping("/deleteConfigMap")
    @ApiOperation("删除configmap")
    public ResultBody deleteConfigMap(String configMapName, String namespace) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.deleteConfigMap(configMapName, namespace));
    }

    @DeleteMapping("/deleteK8sPod")
    @ApiOperation("删除pod")
    public ResultBody deleteK8sPod(String podFile) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.deleteK8sPod(podFile));
    }

    @DeleteMapping("/deleteK8sService")
    @ApiOperation("删除service")
    public ResultBody deleteK8sService(String serviceFile) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.deleteK8sService(serviceFile));
    }

    @GetMapping("/consulCatalogServices")
    @ApiOperation("获取consul所有健康注册服务名称")
    public ResultBody consulCatalogServices(String serviceFile) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.consulCatalogServices(serviceFile));
    }

    @GetMapping("/consulWatchChecksService")
    @ApiOperation("获取单个服务的consul健康注册信息")
    public ResultBody consulWatchChecksService(String serviceFile,String serviceName) throws IOException, InterruptedException {
        return ResultBody.success(k8sDeployService.consulWatchChecksService(serviceFile,serviceName));
    }

    @PostMapping("/autoCompletePublishPodByExecutorService")
    @ApiOperation("自动化完全部署pods（多线程）")
    public ResultBody autoCompletePublishPodByExecutorService(@RequestBody List<String> services){
        return ResultBody.success(k8sDeployService.autoCompletePublishPodByExecutorService(services));
    }

    @PostMapping("/autoSimplePublishPodByExecutorService")
    @ApiOperation("自动化简单部署pods（多线程）")
    public ResultBody autoSimplePublishPodByExecutorService(@RequestBody List<String> services){
        return ResultBody.success(k8sDeployService.autoSimplePublishPodByExecutorService(services));
    }

    @PostMapping("/autoCompletePublishIg")
    @ApiOperation("自动化完全部署集成")
    public ResultBody autoCompletePublishIg() throws IOException, InterruptedException {
        return ResultBody.success(Collections.singletonList(k8sDeployService.autoCompletePublishIg()));
    }

    @PostMapping("/autoSimplePublishIg")
    @ApiOperation("自动化简单部署集成")
    public ResultBody autoSimplePublishIg() throws IOException, InterruptedException {
        return ResultBody.success(Collections.singletonList(k8sDeployService.autoSimplePublishIg()));
    }

    @PostMapping("/autoCompletePublishWeb")
    @ApiOperation("自动化完全部署web")
    public ResultBody autoCompletePublishWeb() throws IOException, InterruptedException {
        return ResultBody.success(Collections.singletonList(k8sDeployService.autoCompletePublishWeb()));
    }

    @PostMapping("/autoSimplePublishWeb")
    @ApiOperation("自动化简单部署web")
    public ResultBody autoSimplePublishWeb() throws IOException, InterruptedException {
        return ResultBody.success(Collections.singletonList(k8sDeployService.autoSimplePublishWeb()));
    }

    @PostMapping("/autoCompletePublishAdmin")
    @ApiOperation("自动化完全部署后管")
    public ResultBody autoCompletePublishAdmin() throws IOException, InterruptedException {
        return ResultBody.success(Collections.singletonList(k8sDeployService.autoCompletePublishAdmin()));
    }

    @PostMapping("/autoSimplePublishAdmin")
    @ApiOperation("自动化简单部署后管")
    public ResultBody autoSimplePublishAdmin() throws IOException, InterruptedException {
        return ResultBody.success(Collections.singletonList(k8sDeployService.autoSimplePublishAdmin()));
    }
}
